package zhh.tangbao.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhh.tangbao.system.annotation.Authority;
import zhh.tangbao.system.annotation.Log;
import zhh.tangbao.system.constant.AuthorityModel;
import zhh.tangbao.system.result.Result;
import zhh.tangbao.user.dto.ModuleQueryDto;
import zhh.tangbao.user.dto.RoleDto;
import zhh.tangbao.user.dto.role.RoleOperateUpdateDto;
import zhh.tangbao.user.dto.role.RoleQueryDto;
import zhh.tangbao.user.entity.AuthorityOperate;
import zhh.tangbao.user.entity.Role;
import zhh.tangbao.user.service.RoleService;
import zhh.tangbao.user.vo.*;

import java.util.List;

/**
 * @author 粥灰灰
 * @version 1.0
 * @created 2022/10/5
 * @Description 描述
 */

@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Autowired
    private RoleService service;

    @PostMapping("/add")
    @Authority(authorities = "role", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "添加角色", moduleName = "角色管理")
    public Result add(@RequestBody Role dto) {
        return Result.successOrFail(service.addRole(dto));
    }

    @DeleteMapping("/delete/{id}")
    @Authority(authorities = "role")
    public Result delete(@PathVariable Long id) {
        return Result.successOrFail(service.deleteRole(id));
    }

    @PostMapping("/update")
    @Authority(authorities = "role")
    public Result update(@RequestBody Role dto) {
        return Result.successOrFail(service.updateRole(dto));
    }

    @GetMapping("/{id}")
    @Authority(authorities = "role")
    public Result findById(@PathVariable String id) {
        return Result.success().put(service.findById(id));
    }

    @GetMapping("/getType")
    @Authority
    public Result getRoleType() {
        List<RoleTypeVo> result = service.getRoleType();
        return Result.success().put(result);
    }

    /**
     * 判断该页面是否有当前用户的功能操作权限
     *
     * @param openAllFunction 是否无需通过权限检查直接放开全部功能
     * @param module          模块名称
     */
    @GetMapping("/getRoleOperate")
    @Authority(login = false)
    public Result getRoleOperate(String module, boolean openAllFunction) {
        List<AuthorityOperate> roleOperate = service.getRoleOperate(module, openAllFunction);
        return Result.success().put(roleOperate);
    }

    @GetMapping("/moduleOption")
    @Authority(login = false)
    public Result getModuleOption(ModuleQueryDto dto) {
        List<ModuleOptionVo> options = service.getModuleOption(dto);
        return Result.success().put(options);
    }

    @GetMapping("/moduleInfo")
    @Authority(login = false)
    public Result getModuleOperates(String moduleCode) {
        ModuleInfoVo vo = service.getModuleOperates(moduleCode);
        return Result.success().put(vo);
    }

    @GetMapping("/page")
    @Authority(login = false)
    public Result page(RoleQueryDto dto) {
        Page<Role> page = service.page(dto);
        return Result.success().put(page);
    }

    @DeleteMapping("/batchDelete")
    @Authority(authorities = "role", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "批量删除角色", moduleName = "角色管理")
    public Result batchDelete(@RequestBody List<String> ids) {
        return Result.successOrFail(service.batchDelete(ids));
    }

    @GetMapping("/roleOperateTree")
    public Result getRoleOperateTree(String roleId) {
        List<OperateTreeNodeVo> vo = service.getRoleOperateTree(roleId);
        return Result.success().put(vo);
    }

    /**
     * 查询该角色拥有的所有权限的树以及全部权限
     */
    @GetMapping("/alreadyHadOperateCode")
    public Result getRoleAlreadyHadOperateCode(String roleId) {
        List<String> vo = service.getRoleAlreadyHadOperateTree(roleId);
        return Result.success().put(vo);
    }

    @GetMapping("/operateTree")
    public Result getOperateTree() {
        List<OperateTreeNodeVo> result = service.getOperateTree();
        return Result.success().put(result);
    }

    @PutMapping("/updateRoleOperate")
    @Authority(authorities = "role", authorityModel = AuthorityModel.CONTAIN)
    @Log(function = "修改角色权限", moduleName = "角色管理")
    public Result updateRoleOperate(@RequestBody RoleOperateUpdateDto dto) {
        return Result.successOrFail(service.updateRoleOperate(dto));
    }
}
